package com.ndpmedia.fb.pmd.configration.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.ndpmedia.fb.pmd.configration.db.AppRepository;
import com.ndpmedia.fb.pmd.configration.db.ConfigRepository;
import com.ndpmedia.fb.pmd.configration.db.PropertiesRepository;
import com.ndpmedia.fb.pmd.configration.model.po.AppPo;
import com.ndpmedia.fb.pmd.configration.model.po.ConfigPo;
import com.ndpmedia.fb.pmd.configration.model.po.PropertiesPo;
import com.ndpmedia.fb.pmd.configration.service.AppService;
import com.ndpmedia.fb.pmd.configration.service.ZooKeeperServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2014/10/12.
 */
@Service
public class AppServiceImpl implements AppService {
    @Autowired
    private AppRepository appRepository;

    @Autowired
    private ConfigRepository configRepository;

    @Autowired
    private PropertiesRepository propertiesRepository;

    @Autowired
    private  ZooKeeperServices zooKeeperServices;

    @Override
    public List list(String key) {
        return appRepository.findAll(key);
    }

    @Override
    public AppPo add(String name) {
        return appRepository.save(name);
    }

    @Override
    @Transactional
    public boolean delete(String appid) {
        propertiesRepository.deleteByappId(appid);
        configRepository.deleteByAppId(appid);
        return appRepository.delete(appid);
    }

    @Override
    public boolean update(String appid, String name) {
        return appRepository.update(appid, name);
    }

    @Override
    public boolean push(String appid) {
        List<ConfigPo> configs = configRepository.findAll(appid);
        ArrayList preps =new ArrayList();
        if (configs != null && configs.size() > 0) {
            for (int i = 0; i < configs.size(); i++) {
                ConfigPo config = configs.get(i);
                String configid = config.getId() + "";
                preps.addAll(propertiesRepository.findAll(appid, configid));
            }
        }

        return zooKeeperServices.configPublish(appid, getConfigMapString(preps));
    }


    public boolean status(String appid, String status) {
        return appRepository.updateStatus(appid, status);
    }

    private String getConfigMapString(ArrayList<PropertiesPo> list){
        Map<String,String> configmap=new HashMap<>();
        for (PropertiesPo  ppo : list) {
            configmap.put(ppo.getKey(),ppo.getValue());
        }

        return  JSON.toJSONString(configmap);
    }

    @Override
    @Transactional
    public boolean copy(String sourceAppId, String destAppId) {

        if(appRepository.findByName(destAppId)){
            return false;
        }

        List<ConfigPo> sourceConfigs = configRepository.findAll(sourceAppId);
        List<ConfigPo> destConfigs = new ArrayList<>();
        AppPo destAppPo =  appRepository.save(destAppId);
        for (int i = 0;i<sourceConfigs.size();i++){
            ConfigPo configPo =  sourceConfigs.get(i);
            ConfigPo newConfigPo = configRepository.save(destAppPo.getAppId(),configPo.getName());
            List<PropertiesPo> oldproplist=propertiesRepository.findAll(sourceAppId, String.valueOf(configPo.getId()));
                for (PropertiesPo propertiesPo : oldproplist) {
                    propertiesRepository.save(destAppPo.getAppId(),newConfigPo.getId().toString(),propertiesPo.getKey(),propertiesPo.getValue(),propertiesPo.getDescription());
                }
        }
        return true;
    }
}
